package com.api;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Repository {

    public File base;
    private List<Class<?>> l;
    private MyClassLoader mcl;

    public Repository(File base) {
        mcl = new MyClassLoader(base);
        this.base = base;
    }

    public List<Class<?>> load(){
        l = new ArrayList<Class<?>>();
        try {
            load(base, null);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, ex);
        }
        return l;
    }
    
    private void load(File f, String chemin) throws ClassNotFoundException{
        for (File f1 : f.listFiles()) {
            if (f1.isDirectory()) {
                if (chemin == null)
                    chemin = f1.getName();
                else
                    chemin = chemin + "." + f1.getName();
                this.load(f1, chemin);
            }
            else if (f1.getName().endsWith(".class")){
                Class<?> c;
                if (chemin == null) {
                	String maChaine = f1.getName();
                	maChaine = maChaine.substring(0, (maChaine.length() - 6));
                    c = mcl.loadClass(maChaine);
                }
                else {
                	String maChaine = chemin + "." + f1.getName();
                	maChaine = maChaine.substring(0, (maChaine.length() - 6));
                    c = mcl.loadClass(maChaine);
                }
                l.add(c);
            }
        }   
    }
}